import { request } from 'umi';
import type { Params, ListItemDataType } from './data.d';
import DJANGO_HOST from '../../../../../config/global_var';

export async function queryCategoryList(): Promise<{data: {list: string[]}}> {
  return request(DJANGO_HOST + "/news/category", {}).then(
    (res) => {
      return {"data": {"list": res.list}};
    }
  );
}

export async function querySourceList(): Promise<{data: {list: string[]}}> {
  return request(DJANGO_HOST + "/news/source", {}).then(
    (res) => {
      return {"data": {"list": res.list}};
    }
  );
}

export async function queryNewsList(
  params: Params,
): Promise<{ data: { list: ListItemDataType[], cont: boolean }}> {
  return request(DJANGO_HOST + '/news/search', {
    params,
  }).then(
    (res) => {
      const list = [];
      const count = res.list.length;
      const cont_ph = res.cont;
      for (let i=0; i < count; i+= 1) {
        var ele = res.list[i];
        list.push({
          time: ele.time,
          category: ele.category,
          source: ele.source,
          url: ele.url,
          title: ele.title,
          content: ele.content,
          id: ele.id,
          href: ele.href,
        })
      }
      console.log(list);
      return {"data": {"list": list, "cont": cont_ph}};
    }
  );
}
